<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/admin_panel/site_admin_ajax_grid_ctrl_ua.class.php');
require_once('vpage_select_grid_ctrl.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');


class VPage_Select_Grid_Ajax_Ctrl extends Site_Admin_Ajax_Grid_Ctrl_UA {
	protected function init_transit_vars() {
		$this->add_transit_var('ret');
		$this->add_transit_var('menu');
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();
		$ret = new vpage_select_Grid_Ctrl('vpage_select', $tvm, Tangra_Parameter_Method::GET);

		$this->process_filter($tvm);
		$this->export_templates();

		return $ret;
	}


	private function process_filter($vm) {
		$context = $this->get__context();
		if ($context->exists_in_get('path')) {
			$path = $context->get_from_get('path');
			$vm->set_page_var('vpage_select', 'path', $path);
		}

		if ($context->exists_in_get('online')) {
			$online = $context->get_from_get('online');
			$vm->set_page_var('vpage_select', 'online', $online);
		}

		if ($context->exists_in_get('template')) {
			$template = $context->get_from_get('template');
			$vm->set_page_var('vpage_select', 'template', $template);
		}

		if ($context->exists_in_get('sys_gen')) {
			$sys_gen = $context->get_from_get('sys_gen');
			$vm->set_page_var('vpage_select', 'sys_gen', $sys_gen);
		}

		$path = $vm->get_page_var('vpage_select', 'path');
		$online = $vm->get_page_var('vpage_select', 'online');
		$template = $vm->get_page_var('vpage_select', 'template');
		$sys_gen = $vm->get_page_var('vpage_select', 'sys_gen');

		$this->export('path', $path);
		$this->export('online', $online);
		$this->export('template', $template);
		$this->export('sys_gen', $sys_gen);

		$ret = $this->get_transit_var('ret');
		$this->export('ret', urldecode($ret));
	}


	private function export_templates() {
	 	$dbc = $this->get_dbc();

		$templates = TCMS_VPage_Template_DBC::load_templates_id_and_name($dbc);
		array_unshift($templates, array('id' => 0, 'name' => '---'));
		$this->export('templates', $templates);
	 }
}